package com.example.application;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.application.model.UserMessage;

import java.util.List;

public class UserMessageAdapter extends RecyclerView.Adapter<UserMessageAdapter.UserMessageViewHolder> {
    private final List<UserMessage> mUserMessageList;

    UserMessageAdapter(List<UserMessage> userMessageList) {
        mUserMessageList = userMessageList;
    }

    @Override
    public int getItemViewType(int position){
        UserMessage msg = mUserMessageList.get(position);
        return msg.getType();
    }

    @NonNull
    @Override
    public UserMessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == UserMessage.TYPE_RECEIVE) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_left, parent, false);
            return new UserMessageViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.msg_right, parent, false);
            return new UserMessageViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull UserMessageViewHolder holder, int position) {
        UserMessage msg = mUserMessageList.get(position);
        if (msg.getType() == UserMessage.TYPE_RECEIVE){
            holder.tvLeft.setText(msg.getContent());
        } else {
            holder.tvRight.setText(msg.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return mUserMessageList.size();
    }

    class UserMessageViewHolder extends RecyclerView.ViewHolder {
        TextView tvLeft, tvRight;

        UserMessageViewHolder(View itemView) {
            super(itemView);

            tvLeft = itemView.findViewById(R.id.user_left);
            tvRight = itemView.findViewById(R.id.user_right);
        }
    }
}
